﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 文件名: V_User.cs
// 作 者：代码自动生成
// 日 期：2011-10-26 11:07:14
// 描 述：
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using Csla;
using lite;
using Csla.Validation;
using Csla.Data;
using System.Data.SqlClient;
using ZX.DAL;

using Csla.Core;
using OpenExpressApp.ORM;
using OpenExpressApp.MetaAttribute;
using OpenExpressApp.DB.Utils;
using OpenExpressApp;
namespace ZX.EHR.Library
{
	/// <summary>
    /// 
    /// </summary>
	[Serializable]
    [Table(Name = "V_User")]
    [ZX.DBModule.Table("V_User","UserID")]
	public partial class V_User: ZX.DBModule.BaseEntity
	{
        #region Business Methods 业务方法
        [Column]
        public string LDeptName
        {
            get;
            set;
        }

        [Column]
        public string DeptPath
        {
            get;
            set;
        }
		/// <summary>
        /// 用户ID
        /// </summary>
        private static PropertyInfo< Guid > UserIDProperty = RegisterProperty(typeof(V_User),new PropertyInfo< Guid >("UserID"));
        [DataObjectField(true, true)]
        [Column, PK]
        public Guid UserID
        {
            get { return GetProperty(UserIDProperty); }
            set { SetProperty(UserIDProperty, value); }
        }
        
		/// <summary>
        /// 职员ID
        /// </summary>
        private static PropertyInfo< Guid > EmpIDProperty = RegisterProperty(typeof(V_User),new PropertyInfo< Guid >("EmpID"));
        [Column]
        public Guid EmpID
        {
            get { return GetProperty(EmpIDProperty); }
            set { SetProperty(EmpIDProperty, value); }
        }
        /// <summary>
        /// 客户ID
        /// </summary>
        private static PropertyInfo<Guid> CustomerIDProperty = RegisterProperty(typeof(V_User), new PropertyInfo<Guid>("CustomerID"));
        [Column]
        public Guid CustomerID
        {
            get { return GetProperty(CustomerIDProperty); }
            set { SetProperty(CustomerIDProperty, value); }
        }
		/// <summary>
        /// 登陆名
        /// </summary>
        private static PropertyInfo< string > LoginNameProperty = RegisterProperty(typeof(V_User),new PropertyInfo< string >("LoginName"));
        [Column]
        public string LoginName
        {
            get { return GetProperty(LoginNameProperty); }
            set { SetProperty(LoginNameProperty, value); }
        }
		/// <summary>
        /// 名称
        /// </summary>
        private static PropertyInfo< string > UNameProperty = RegisterProperty(typeof(V_User),new PropertyInfo< string >("UName"));
        [Column]
        public string UName
        {
            get { return GetProperty(UNameProperty); }
            set { SetProperty(UNameProperty, value); }
        }
		/// <summary>
        /// 邮箱
        /// </summary>
        private static PropertyInfo< string > EmailProperty = RegisterProperty(typeof(V_User),new PropertyInfo< string >("Email"));
        [Column]
        public string Email
        {
            get { return GetProperty(EmailProperty); }
            set { SetProperty(EmailProperty, value); }
        }
		/// <summary>
        /// 密码
        /// </summary>
        private static PropertyInfo< string > PasswordProperty = RegisterProperty(typeof(V_User),new PropertyInfo< string >("Password"));
        [Column]
        public string Password
        {
            get { return GetProperty(PasswordProperty); }
            set { SetProperty(PasswordProperty, value); }
        }
		/// <summary>
        /// 
        /// </summary>
        private static PropertyInfo< DateTime > CreateDateProperty = RegisterProperty(typeof(V_User),new PropertyInfo< DateTime >("CreateDate"));
        [Column]
        public DateTime CreateDate
        {
            get { return GetProperty(CreateDateProperty); }
            set { SetProperty(CreateDateProperty, value); }
        }
		/// <summary>
        /// true外部用户，false/null 内部用户
        /// </summary>
        private static PropertyInfo< bool > IsOutsiteProperty = RegisterProperty(typeof(V_User),new PropertyInfo< bool >("IsOutsite"));
        [Column]
        public bool IsOutsite
        {
            get { return GetProperty(IsOutsiteProperty); }
            set { SetProperty(IsOutsiteProperty, value); }
        }
		/// <summary>
        /// 生效日期
        /// </summary>
        private static PropertyInfo< DateTime > EffectDateProperty = RegisterProperty(typeof(V_User),new PropertyInfo< DateTime >("EffectDate"));
        [Column]
        public DateTime EffectDate
        {
            get { return GetProperty(EffectDateProperty); }
            set { SetProperty(EffectDateProperty, value); }
        }
		/// <summary>
        /// 失效日期
        /// </summary>
        private static PropertyInfo< DateTime > ExpireDateProperty = RegisterProperty(typeof(V_User),new PropertyInfo< DateTime >("ExpireDate"));
        [Column]
        public DateTime ExpireDate
        {
            get { return GetProperty(ExpireDateProperty); }
            set { SetProperty(ExpireDateProperty, value); }
        }
		/// <summary>
        /// 
        /// </summary>
        private static PropertyInfo< bool > IsExpireProperty = RegisterProperty(typeof(V_User),new PropertyInfo< bool >("IsExpire"));
        [Column]
        public bool IsExpire
        {
            get { return GetProperty(IsExpireProperty); }
            set { SetProperty(IsExpireProperty, value); }
        }
		/// <summary>
        /// USB ikey SerialNum
        /// </summary>
        private static PropertyInfo< string > IkeySNProperty = RegisterProperty(typeof(V_User),new PropertyInfo< string >("IkeySN"));
        [Column]
        public string IkeySN
        {
            get { return GetProperty(IkeySNProperty); }
            set { SetProperty(IkeySNProperty, value); }
        }
		/// <summary>
        /// 是否启用iKey(1:启用;0:不启用)
        /// </summary>
        private static PropertyInfo< bool > IsOnKeyProperty = RegisterProperty(typeof(V_User),new PropertyInfo< bool >("IsOnKey"));
        [Column]
        public bool IsOnKey
        {
            get { return GetProperty(IsOnKeyProperty); }
            set { SetProperty(IsOnKeyProperty, value); }
        }

        [Column]
        public Guid? AddUser
        {
            get;
            set;
        }

        [Column]
        public DateTime? AddTime
        {
            get;
            set;
        }

        /// <summary>
        /// 默认角色【0-外部客户 1-外部客户负责人】
        /// </summary>
        private static PropertyInfo<string> IsDefaultRoleNameProperty = RegisterProperty(typeof(V_User), new PropertyInfo<string>("IsDefaultRoleName"));
        [Column]
        public string IsDefaultRoleName
        {
            get { return GetProperty(IsDefaultRoleNameProperty); }
            set { SetProperty(IsDefaultRoleNameProperty, value); }
        }
        #endregion
        
        #region Validation Rules 验证规则
        //将验证规则与业务对象的属性联系在一起
        protected override void AddBusinessRules()
        {
            //AddRule的第一个参数是委托变量，也就是说是方法的变量
            ValidationRules.AddRule(CommonRules.StringRequired, "LoginName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("LoginName", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "UName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("UName", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "Email");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Email", 255));
            ValidationRules.AddRule(CommonRules.StringRequired, "Password");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Password", 50));
            ValidationRules.AddRule(CommonRules.StringRequired, "IkeySN");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("IkeySN", 50));
            //base.AddBusinessRules();
        }
        #endregion
        
        #region Factory Methods 工厂方法

        public V_User(){}

        public static V_User New()
        {
            return DataPortal.Create< V_User>();
        }
        public static V_User Get(Guid UserID)
        {
            return DataPortal.Fetch< V_User>(new SingleLazyCriteria< V_User, Guid>(UserID, false));
        }
        public static void Delete(Guid UserID)
        {
            DataPortal.Delete(UserID);
        }

        //public override V_User Save()
        //{
            //return base.Save();
        //}
        #endregion
		
		#region Data Access 数据访问
        //由于DataPortal_create中没有对数据库的访问，所有标记了Runlocal特性，使得该方法只需在本地运行即可
        //如果代码中有访问数据库的方法，那么就不要用这个特性，使数据门户可以根据情况将此方法的运行选择在应用服务器还是本地
        [RunLocal()]
        protected override void DataPortal_Create()
        {
            //在此调用CheckRules对所有的属性进行验证规则的检查，这样检查只运行一次。
            ValidationRules.CheckRules();
        }


        //可以看到此方法没有标记任何特性
        //此方法要访问数据库所以不能标记本地，由于她没有任何数据库更新操作所以也无需事务性的保护
        //此方法调用结束后会隐含的调用MarkOld（）方法
        private void DataPortal_Fetch(Guid UserID)
        {
            //通过连接字符串建立SQL连接对象，Using符号代表，范围释放的意思，SqlConnection对象会在此范围后被释放掉
             using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                IQuery q = db.Query();
                q.Constrain("UserID").Equal(UserID);
                var data = db.Select< V_User>(q)[0];
                LoadProperty(UserIDProperty, data.UserID);
                LoadProperty(EmpIDProperty, data.EmpID);
                LoadProperty(CustomerIDProperty, data.CustomerID);
                LoadProperty(LoginNameProperty, data.LoginName);
                LoadProperty(UNameProperty, data.UName);
                LoadProperty(EmailProperty, data.Email);
                LoadProperty(PasswordProperty, data.Password);
                LoadProperty(CreateDateProperty, data.CreateDate);
                LoadProperty(IsOutsiteProperty, data.IsOutsite);
                LoadProperty(EffectDateProperty, data.EffectDate);
                LoadProperty(ExpireDateProperty, data.ExpireDate);
                LoadProperty(IsExpireProperty, data.IsExpire);
                LoadProperty(IkeySNProperty, data.IkeySN);
                LoadProperty(IsOnKeyProperty, data.IsOnKey);
                LoadProperty(IsDefaultRoleNameProperty, data.IsDefaultRoleName);
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            //依然是调用下面的方法删除的，只是他此时可以获得自己的Id
            DataPortal_Delete(UserIDProperty);
        }
        #endregion

        [Serializable]
        public class EmpDefault_Value : ZX.DBModule.BaseEntity
        {
            [lite.Column]
            public Guid? UserID
            {
                get;
                set;
            }
            [lite.Column]
            public string Proposer
            { get; set; }
            [lite.Column]
            public string DeptID
            {
                get;
                set;
            }
            [lite.Column]
            public string DeptName
            { get; set; }

            [lite.Column]
            public string PostID
            {
                get;
                set;
            }
            [lite.Column]
            public string PostName
            {
                get;
                set;
            }
            [lite.Column]
            public string FilialeID
            {
                get;
                set;
            }
            [lite.Column]
            public string Filiale
            {
                get;
                set;
            }
            [lite.Column]
            public string CreatedTime
            {
                get;
                set;
            }

            public EmpDefault_Value(Guid UserID)
            {
                
                IResultSet dt = null;
                using (var db = DBHelper.CreateDb(ZX.DAL.ConnectionStringNames.OpenExpressApp))
                {
                    dt = db.Exec("sp_workflowAndformdefaultbyUser", new object[] { UserID });
                    if (dt.Rows > 0) 
                    {
                        object[] dr = dt.GetRow(0);
                        this.UserID = (Guid)dr[0];
                        this.Proposer=(dr[1]==null)?"":dr[1].ToString();
                        this.DeptID = (dr[2] == null) ? "" : dr[2].ToString();
                        this.DeptName = (dr[3] == null) ? "" : dr[3].ToString();
                        this.PostID = (dr[4] == null) ? "" : dr[4].ToString();
                        this.PostName = (dr[5] == null) ? "" : dr[5].ToString();
                        this.FilialeID = (dr[6] == null) ? "" : dr[6].ToString();
                        this.Filiale = (dr[7] == null) ? "" : dr[7].ToString();
                        this.CreatedTime = DateTime.Now.ToString();

                    }
                }
                
            }
        }
	}
}

